package com.yx.config;

import com.yx.constatns.RabbitMQConstatns;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname RabbitMQConfig
 * @Date 2021/07/27 15:25
 * @Author by yx
 * @Description TODO:
 */
@Configuration
public class StockRabbitMQConfig {

    @Bean
    public TopicExchange stockExchange(){
        return new TopicExchange(RabbitMQConstatns.STOCK_EXCHANGE, true, false,null);
    }

    @Bean
    public Queue stockQueue(){
        Map<String,Object> args = new HashMap<>();
        // 绑定死信队列
        args.put("x-dead-letter-exchange", RabbitMQConstatns.STOCK_DLE_EXCHANGE);
        args.put("x-dead-letter-routing-key", "dle.stock.err");
        args.put("x-message-ttl", 10000*6*2);   // 超时时间是2min
        return new Queue(RabbitMQConstatns.STOCK_QUEUE, true, false, false,args);
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(stockQueue()).to(stockExchange()).with("shop.stock.*");
    }

    /*
     * 死信队列
     * */
    @Bean
    public TopicExchange stockDLEExchange(){
        return new TopicExchange(RabbitMQConstatns.STOCK_DLE_EXCHANGE, true, false,null);
    }

    @Bean
    public Queue stockDLEQueue(){
        return new Queue(RabbitMQConstatns.STOCK_DLE_QUEUE, true, false, false);
    }

    @Bean
    public Binding stockDLEBinding(){
        return BindingBuilder.bind(stockDLEQueue()).to(stockDLEExchange()).with("dle.stock.*");
    }
}
